﻿#include "cwebrequestapi.h"
#include "qjsonarray.h"

#include <QCoreApplication>
#include <QNetworkAccessManager>
#include <QNetworkRequest>
#include <QNetworkReply>
#include <QJsonObject>
#include <QJsonDocument>
#include <QJsonArray>

#include <login/cuserinfomgr.h>

CWebRequestApi::CWebRequestApi(QObject *parent)
    : QObject{parent} {}

CWebRequestApi &CWebRequestApi::ins()
{
    static CWebRequestApi api;
    return api;
}

bool CWebRequestApi::sendBlockPostRequest(const QString &url, const QJsonObject &requestBody, QJsonObject &responseBody) {
    CUserInfoMgr& mgr = CUserInfoMgr::ins();

    QNetworkAccessManager manager;
    QNetworkRequest request(url);
    request.setHeader(QNetworkRequest::ContentTypeHeader, "application/json");
    request.setRawHeader("token", mgr.token().toUtf8());

    QByteArray data = QJsonDocument(requestBody).toJson(QJsonDocument::Compact);

    QNetworkReply *reply = manager.post(request, data);

    while (!reply->isFinished()) {
        QCoreApplication::processEvents();
    }

    if (reply->error() == QNetworkReply::NoError) {
        QByteArray responseData = reply->readAll();
        QJsonDocument jsonResponse = QJsonDocument::fromJson(responseData);
        if (jsonResponse.isObject()) {
            responseBody = jsonResponse.object();
            reply->deleteLater();
            return true;
        }
    }

    reply->deleteLater();
    return false;
}

bool CWebRequestApi::sendBlockGetRequest(const QString &url, QJsonObject &responseBody) {
    CUserInfoMgr& mgr = CUserInfoMgr::ins();

    QNetworkAccessManager manager;
    QNetworkRequest request(url);
    request.setHeader(QNetworkRequest::ContentTypeHeader, "application/json");
    request.setRawHeader("Content-Type", "application/json");
    request.setRawHeader("token", mgr.token().toUtf8());

    QNetworkReply *reply = manager.get(request);

    while (!reply->isFinished()) {
        QCoreApplication::processEvents();
    }

    if (reply->error() == QNetworkReply::NoError) {
        QByteArray responseData = reply->readAll();
        QJsonDocument jsonResponse = QJsonDocument::fromJson(responseData);
        if (jsonResponse.isObject()) {
            responseBody = jsonResponse.object();
            reply->deleteLater();
            return true;
        }
    }

    reply->deleteLater();
    return false;
}

bool CWebRequestApi::getModelList(QList<QSharedPointer<CModelInfo> > &modelList) {
#if TEST_MODEL
    CUserInfoMgr& mgr = CUserInfoMgr::ins();
    QJsonObject resquestBody;
    QJsonObject response;

    resquestBody[userFieldName(USER_ENUM::EUserId)] = mgr.info().userid;
    resquestBody["type"] = "rt";
    if(!sendBlockPostRequest(ADDR + "/api/model/getModels", resquestBody, response))
        return false;

    QJsonArray models = response["models"].toArray();

    for(auto model : models) {
        QSharedPointer<CModelInfo> info(new CModelInfo);
        info->fromJson(model.toObject());
        modelList.append(info);
    }
#else
    for(int i = 0; i < 50; i++) {
        QJsonObject obj;
        obj[mdoelFieldName(MODEL_ENUM::EName)] = QString("model%1").arg(i);
        obj[mdoelFieldName(MODEL_ENUM::EUser)] = QString("user%1").arg(i);
        obj[mdoelFieldName(MODEL_ENUM::EDescr)] = QString("这是模型%1").arg(i);
        obj[mdoelFieldName(MODEL_ENUM::ESn)] = i;
        obj[mdoelFieldName(MODEL_ENUM::EGroupIngs)] = QString("分组%1").arg(i%5);
        obj[mdoelFieldName(MODEL_ENUM::ELocation)] = QString("location%1").arg(i);
        obj[mdoelFieldName(MODEL_ENUM::ECreate_time)] = QDateTime::currentDateTime().toString();
        obj[mdoelFieldName(MODEL_ENUM::EUpdate_time)] = QDateTime::currentDateTime().toString();
        obj[mdoelFieldName(MODEL_ENUM::EProducer)] = QString("producer%1").arg(i);
        obj[mdoelFieldName(MODEL_ENUM::EValid)] = true;
        obj[mdoelFieldName(MODEL_ENUM::EConfigs)] = QJsonObject();
        QSharedPointer<CModelInfo> info(new CModelInfo);
        info->fromJson(obj);
        modelList.append(info);
    }
#endif
    return true;
}

bool CWebRequestApi::addModel(QSharedPointer<CModelInfo> &info)
{
    QJsonObject response;
    QJsonObject resquestBody = info->toJson();
    if(!sendBlockPostRequest(ADDR + "/api/model/addModel", resquestBody, response))
        return false;

    if(response["status"].toString() != "success")
        return false;

    return true;
}

bool CWebRequestApi::addDevice(CRtDeviceInfo &info)
{
    QJsonObject response;
    QJsonObject resquestBody = info.toJson();
    if(!sendBlockPostRequest(ADDR + "/api/model/addDevice", resquestBody, response))
        return false;

    if(response["state"].toObject()["status"].toString() != "success")
        return false;

    return true;
}

bool CWebRequestApi::getDevicesByModel(const QString userid, const QString modelName,  QList<CRtDeviceInfo> &devlist, bool is_rt)
{
    if(modelName.isEmpty() || userid.isEmpty())
        return false;

    QJsonObject resquestBody;
    resquestBody[userFieldName(USER_ENUM::EUserId)] = userid;
    resquestBody[TYPE] = DATABASE;
    resquestBody[baseSigFieldName(BASE_SIG_ENUM::EModel_name)] = modelName;

    QJsonObject response;

    if(!sendBlockPostRequest(ADDR + "/api/model/getDevicesByModel", resquestBody, response))
        return false;

    QJsonArray devsarr = response["devs"].toArray();
    for(auto it : devsarr)   {
        CRtDeviceInfo info;
        info.fromJson(it.toObject());
        devlist.append(info);
    }
    return true;
}

bool CWebRequestApi::addYx(QSharedPointer<CRtModelYxInfo> &yx) {
    QJsonObject response;
    QJsonObject resquestBody = yx->toJson();
    return sendBlockPostRequest(ADDR + "/api/model/addModelYx", resquestBody, response);

    if(response["status"].toString() != "success")
        return false;

    return true;
}

bool CWebRequestApi::changeYx(QSharedPointer<IModel> &yx) {
    QJsonObject resquestBody = yx->toJson();
    return sendBlockPostRequest("http://192.168.56.100:9999/model/changeYx", resquestBody, resquestBody);
}

bool CWebRequestApi::getModelYxList(QList<QSharedPointer<IModel>> &yxList, QString modelName, QStringList yxs)
{

    if(modelName.isEmpty())
        return false;

    QJsonObject resquestBody;
    if(yxs.empty()) {
        resquestBody["yxs"] = QJsonArray();
    } else {
        QJsonArray arr;
        for(QString name : yxs) {
            arr.append(name);
        }
        resquestBody["yxs"] = arr;
    }

    CUserInfoMgr& mgr = CUserInfoMgr::ins();
    resquestBody[userFieldName(USER_ENUM::EUserId)] = mgr.info().userid;
    resquestBody[TYPE] = DATABASE;
    resquestBody[baseSigFieldName(BASE_SIG_ENUM::EModel_name)] = modelName;

    QJsonObject response;

#if TEST_MODEL_YX
    if(!sendBlockPostRequest(ADDR + "/api/model/getModelYxs", resquestBody, response))
        return false;
#else
    QJsonArray arr;
    for(int i = 0; i < SIMULATION_SIG_NUM; ++i) {
        QJsonObject yxJson;
        yxJson[baseSigFieldName(BASE_SIG_ENUM::EModel_name)] = modelName;
        yxJson[baseSigFieldName(BASE_SIG_ENUM::EUser)] = "apple";
        yxJson[baseSigFieldName(BASE_SIG_ENUM::EName)] = QString("test_yx%1").arg(i);
        yxJson[baseSigFieldName(BASE_SIG_ENUM::EDescr)] = QString("descr_yx%1").arg(i);
        yxJson[baseSigFieldName(BASE_SIG_ENUM::ESn)] = i;
        yxJson[baseSigFieldName(BASE_SIG_ENUM::EGrouping)] = QString("group_yx%1").arg(i);
        QString jsonString = R"({
        "name": "张三",
        "age": 30,
        "city": "北京"
        })";
        yxJson[baseSigFieldName(BASE_SIG_ENUM::EConfigs)] = QJsonDocument::fromJson(jsonString.toUtf8()).object();

        QJsonArray valuearr;
        for(int j = 0; j < 5; j++) {
            QJsonObject valueobj;
            valueobj["value"] = j;
            valueobj["v_descr"] = QString("值%1描述").arg(QString::number(j));
            valueobj["v_image"] = QString("值%1图像").arg(QString::number(j));
            valuearr.append(valueobj);
        }
        yxJson[yxFieldName(YX_ENUM::EValues)] = valuearr;
        arr.append(yxJson);
    }
    response["yxs"] = arr;
#endif

    QJsonArray responseYxs = response["yxs"].toArray();
    for(auto it : responseYxs) {
        QSharedPointer<IModel> info(new CRtModelYxInfo());
        info->fromJson(it.toObject());
        yxList.append(info);
    }

    return true;
}

bool CWebRequestApi::addYc(QSharedPointer<CRtModelYcInfo> &yc)
{
    QJsonObject response;
    QJsonObject resquestBody = yc->toJson();
    return sendBlockPostRequest(ADDR + "/api/model/addModelYc", resquestBody, response);
    if(response["status"].toString() != "success")
        return false;

    return true;
}

bool CWebRequestApi::changeYc(QSharedPointer<IModel> &yc) {
    QJsonObject resquestBody = yc->toJson();
    return sendBlockPostRequest("http://192.168.56.100:9999/model/changeYc", resquestBody, resquestBody);
}

bool CWebRequestApi::getModelYcList(QList<QSharedPointer<IModel> > &ycList, QString modelName, QStringList ycs)
{

    if(modelName.isEmpty())
        return false;

    QJsonObject resquestBody;
    if(ycs.empty()) {
        resquestBody["ycs"] = QJsonArray();
    } else {
        QJsonArray arr;
        for(QString name : ycs) {
            arr.append(name);
        }
        resquestBody["ycs"] = arr;
    }

    CUserInfoMgr& mgr = CUserInfoMgr::ins();
    resquestBody[userFieldName(USER_ENUM::EUserId)] = mgr.info().userid;
    resquestBody[TYPE] = DATABASE;
    resquestBody[baseSigFieldName(BASE_SIG_ENUM::EModel_name)] = modelName;

    QJsonObject response;

#if TEST_MODEL_YC
    if(!sendBlockPostRequest(ADDR + "/api/model/getModelYcs", resquestBody, response))
        return false;
#else
    QJsonArray arr;
    for(int i = 0; i < SIMULATION_SIG_NUM; ++i) {
        QJsonObject ycJson;
        ycJson[baseSigFieldName(BASE_SIG_ENUM::EModel_name)] = modelName;
        ycJson[baseSigFieldName(BASE_SIG_ENUM::EUser)] = "apple";
        ycJson[baseSigFieldName(BASE_SIG_ENUM::EName)] = QString("test_yc%1").arg(i);
        ycJson[baseSigFieldName(BASE_SIG_ENUM::EDescr)] = QString("descr_yc%1").arg(i);
        ycJson[baseSigFieldName(BASE_SIG_ENUM::ESn)] = i;
        ycJson[baseSigFieldName(BASE_SIG_ENUM::EGrouping)] = QString("group_yc%1").arg(i);
        QString jsonString = R"({
        "name": "张三",
        "age": 30,
        "city": "北京"
        })";
        ycJson[baseSigFieldName(BASE_SIG_ENUM::EConfigs)] = QJsonDocument::fromJson(jsonString.toUtf8()).object();
        ycJson[ycFieldName(YC_ENUM::EBase)] = i+0.5;
        ycJson[ycFieldName(YC_ENUM::ECoef)] = i+0.5;
        ycJson[ycFieldName(YC_ENUM::EEn_alarm)] = true;
        ycJson[ycFieldName(YC_ENUM::EAlarm_up_limit)] = i+0.5;
        ycJson[ycFieldName(YC_ENUM::EAlarm_down_limit)] = i+0.5;
        ycJson[ycFieldName(YC_ENUM::EAlarm_up_limit_desc)] = QString("down_desc_yc%1").arg(i);
        ycJson[ycFieldName(YC_ENUM::EAlarm_down_limit_desc)] = QString("up_desc_yc%1").arg(i);
        ycJson[ycFieldName(YC_ENUM::EAlarm_time_count)] = i;
        arr.append(ycJson);
    }
    response["ycs"] = arr;
#endif

    QJsonArray responseYcs = response["Ycs"].toArray();
    for(auto it : responseYcs) {
        QSharedPointer<IModel> info(new CRtModelYcInfo());
        info->fromJson(it.toObject());
        ycList.append(info);
    }

    return true;
}

bool CWebRequestApi::addYk(QSharedPointer<CRtModelYkInfo> &yk)
{
    QJsonObject response;
    QJsonObject resquestBody = yk->toJson();
    return sendBlockPostRequest(ADDR + "/api/model/addModelYk", resquestBody, response);
    if(response["state"].toObject()["status"].toString() != "success")
        return false;

    return true;
}

bool CWebRequestApi::changeYk(QSharedPointer<IModel> &yk)
{
    return true;
}

bool CWebRequestApi::getModelYkList(QList<QSharedPointer<IModel> > &ykList, QString modelName, QStringList yks)
{
    if(modelName.isEmpty())
        return false;

    QJsonObject resquestBody;
    if(yks.empty()) {
        resquestBody["yks"] = QJsonArray();
    } else {
        QJsonArray arr;
        for(QString name : yks) {
            arr.append(name);
        }
        resquestBody["yks"] = arr;
    }

    CUserInfoMgr& mgr = CUserInfoMgr::ins();
    resquestBody[userFieldName(USER_ENUM::EUserId)] = mgr.info().userid;
    resquestBody[TYPE] = DATABASE;
    resquestBody[baseSigFieldName(BASE_SIG_ENUM::EModel_name)] = modelName;


    QJsonObject response;
    if(!sendBlockPostRequest(ADDR + "/api/model/getModelYks", resquestBody, response))
        return false;

    QJsonArray responseYks = response["yks"].toArray();

    for(auto it : responseYks) {
        QSharedPointer<IModel> info(new CRtModelYkInfo());
        info->fromJson(it.toObject());
        ykList.append(info);
    }

    return true;
}

bool CWebRequestApi::addParameter(QSharedPointer<CRtModelParameterInfo> &parameter)
{
    QJsonObject response;
    QJsonObject resquestBody = parameter->toJson();
    return sendBlockPostRequest(ADDR + "/api/model/addModelParameter", resquestBody, response);
    if(response["status"].toString() != "success")
        return false;

    return true;
}

bool CWebRequestApi::changeParameter(QSharedPointer<IModel> &parameter)
{

}

bool CWebRequestApi::getModelParameterList(QList<QSharedPointer<IModel> > &parameterList, QString modelName, QStringList parameters)
{
    if(modelName.isEmpty())
        return false;

    QJsonObject resquestBody;
    if(parameters.empty()) {
        resquestBody["parameters"] = QJsonArray();
    } else {
        QJsonArray arr;
        for(QString name : parameters) {
            arr.append(name);
        }
        resquestBody["parameters"] = arr;
    }

    CUserInfoMgr& mgr = CUserInfoMgr::ins();
    resquestBody[userFieldName(USER_ENUM::EUserId)] = mgr.info().userid;
    resquestBody[TYPE] = DATABASE;
    resquestBody[baseSigFieldName(BASE_SIG_ENUM::EModel_name)] = modelName;

    QJsonObject response;
    if(!sendBlockPostRequest(ADDR + "/api/model/getModelParameters", resquestBody, response))
        return false;


    QJsonArray responseParameters = response["parameters"].toArray();

    for(auto it : responseParameters) {
        QSharedPointer<IModel> info(new CRtModelParameterInfo());
        info->fromJson(it.toObject());
        parameterList.append(info);
    }

    return true;
}

bool CWebRequestApi::removeSig( const QList<QSharedPointer<IModel> > &sigs, QStringList &faildnames, ESigType type) {
    if(sigs.size() < 1)
        return true;

    QJsonObject resquestBody;
    resquestBody[baseSigFieldName(BASE_SIG_ENUM::EModel_name)] = sigs.at(0)->getFieldValueByFielddName(baseSigColFieldName(BASE_SIG_ENUM::EModel_name)).toString();
    resquestBody[baseSigFieldName(BASE_SIG_ENUM::EUser)] = sigs.at(0)->getFieldValueByFielddName(baseSigColFieldName(BASE_SIG_ENUM::EUser)).toString();

    QString requestAddr;
    switch (type) {
    case ESigType::EYx:
        requestAddr = "/api/model/delModelYx";
        break;
    case ESigType::EYc:
        requestAddr = "/api/model/delModelYc";
        break;
    case ESigType::EYk:
        requestAddr = "/api/model/delModelYk";
        break;
    case ESigType::EParameter:
        requestAddr = "/api/model/delModelParameter";
        break;
    default:
        return false;
    }

    for(int row = 0; row < sigs.size(); ++row) {
        QString name = sigs.at(row)->getFieldValueByFielddName(baseSigColFieldName(BASE_SIG_ENUM::EUser)).toString();
        resquestBody[baseSigFieldName(BASE_SIG_ENUM::EName)] = name;

        QJsonObject response;

        if(!sendBlockPostRequest(ADDR + requestAddr, resquestBody, response)){
            faildnames.append(name);
            continue;
        }

        if(response["status"].toString() != "success")
            faildnames.append(name);
    }
    return true;
}

bool CWebRequestApi::addModbusMap(CModbusMapInfo &info) {
    QJsonObject response;
    if(!sendBlockPostRequest(ADDR + "/api/protocol/addModbusMap", info.toJson(), response))
        return false;

    return true;
}

bool CWebRequestApi::getModelModbusMaps(const QString userid, const QString modelName, QList<CModbusMapInfo> &infos) {
    QJsonObject resquestBody;
    CUserInfoMgr& mgr = CUserInfoMgr::ins();
    resquestBody[userFieldName(USER_ENUM::EUserId)] = mgr.info().userid;
    resquestBody[baseSigFieldName(BASE_SIG_ENUM::EModel_name)] = modelName;

    QJsonObject response;
    if(!sendBlockPostRequest(ADDR + "/api/protocol/getModbusMaps", resquestBody, response))
        return false;

    QJsonArray maps = response["maps"].toArray();

    for(auto it : maps) {
        CModbusMapInfo info;
        info.fromJson(it.toObject());
        infos.append(info);
    }
    return true;
}

bool CWebRequestApi::getModbusMapYxs(const QString userid, const QString modelName, QString mapid, QList<QSharedPointer<IModel> > &sigs) {
    QJsonObject resquestBody;
    resquestBody[userFieldName(USER_ENUM::EUserId)] = userid;
    resquestBody[baseSigFieldName(BASE_SIG_ENUM::EModel_name)] = modelName;
    resquestBody["map_id"] = mapid;

    QJsonObject response;
    if(!sendBlockPostRequest(ADDR + "/api/protocol/getModbusMapYxs", resquestBody, response))
        return false;

    QJsonArray maps = response["mapsigs"].toArray();
    for(auto it : maps) {
        QSharedPointer<IModel> info(new CModbusUnitCoil(ESigType::EYx));
        info->fromJson(it.toObject());
        sigs.append(info);
    }

    return true;
}

bool CWebRequestApi::changeModbusMapYxs(QString mapid, QMap<QString, QSharedPointer<IModel>> &sigs) {
    QJsonArray array;
    for(auto& it : sigs) {
        array.append(it->toJson());
    }
    QJsonObject resquestBody;
    resquestBody["map_id"] = mapid;
    resquestBody["mapsigs"] = array;

    QJsonObject response;
    if(!sendBlockPostRequest(ADDR + "/api/protocol/changeModbusMapYxs", resquestBody, response))
        return false;

    QJsonObject faild_info = response["faild_info"].toObject();

    bool opt_add = true;
    bool opt_change = true;
    bool opt_delete = true;
    if(faild_info.contains("opt_add")) {
        opt_add = faild_info["opt_add"].toBool();
    }
    if(faild_info.contains("opt_change")) {
        opt_change = faild_info["opt_change"].toBool();
    }
    if(faild_info.contains("opt_delete")) {
        opt_delete = faild_info["opt_delete"].toBool();
    }

    for(auto& it : sigs) {
        CModbusUnitCoil* info = static_cast<CModbusUnitCoil*>(it.data());
        if(info->opt_type == EOptType::EAdd) {
            if(opt_add) {
                info->commit();
            }
        } else if(info->opt_type == EOptType::EChange) {
            if(opt_change) {
                info->commit();
            }
        } else if(info->opt_type == EOptType::EDelete) {
            if(opt_delete) {
                info->deleteReset();
            }
        }
    }

    return true;
}

bool CWebRequestApi::getModbusMapYcs(const QString userid, const QString modelName, QString mapid, QList<QSharedPointer<IModel> > &sigs)
{
    QJsonObject resquestBody;
    resquestBody[userFieldName(USER_ENUM::EUserId)] = userid;
    resquestBody[baseSigFieldName(BASE_SIG_ENUM::EModel_name)] = modelName;
    resquestBody["map_id"] = mapid;

    QJsonObject response;
    if(!sendBlockPostRequest(ADDR + "/api/protocol/getModbusMapYcs", resquestBody, response))
        return false;

    QJsonArray maps = response["mapsigs"].toArray();
    for(auto it : maps) {
        QSharedPointer<IModel> info(new CModbusUnitRegister(ESigType::EYc));
        info->fromJson(it.toObject());
        sigs.append(info);
    }

    return true;
}

bool CWebRequestApi::changeModbusMapYcs(QString mapid, QMap<QString, QSharedPointer<IModel> > &sigs)
{
    QJsonArray array;
    for(auto& it : sigs) {
        array.append(it->toJson());
    }
    QJsonObject resquestBody;
    resquestBody["map_id"] = mapid;
    resquestBody["mapsigs"] = array;

    QJsonObject response;
    if(!sendBlockPostRequest(ADDR + "/api/protocol/changeModbusMapYcs", resquestBody, response))
        return false;

    QJsonObject faild_info = response["faild_info"].toObject();

    bool opt_add = true;
    bool opt_change = true;
    bool opt_delete = true;
    if(faild_info.contains("opt_add")) {
        opt_add = faild_info["opt_add"].toBool();
    }
    if(faild_info.contains("opt_change")) {
        opt_change = faild_info["opt_change"].toBool();
    }
    if(faild_info.contains("opt_delete")) {
        opt_delete = faild_info["opt_delete"].toBool();
    }

    for(auto& it : sigs) {
        CModbusUnitRegister* info = static_cast<CModbusUnitRegister*>(it.data());
        if(info->opt_type == EOptType::EAdd) {
            if(opt_add) {
                info->commit();
            }
        } else if(info->opt_type == EOptType::EChange) {
            if(opt_change) {
                info->commit();
            }
        } else if(info->opt_type == EOptType::EDelete) {
            if(opt_delete) {
                info->deleteReset();
            }
        }
    }

    return true;
}

bool CWebRequestApi::getModbusMapYks(const QString userid, const QString modelName, QString mapid, QList<QSharedPointer<IModel> > &sigs)
{
    QJsonObject resquestBody;
    resquestBody[userFieldName(USER_ENUM::EUserId)] = userid;
    resquestBody[baseSigFieldName(BASE_SIG_ENUM::EModel_name)] = modelName;
    resquestBody["map_id"] = mapid;

    QJsonObject response;
    if(!sendBlockPostRequest(ADDR + "/api/protocol/getModbusMapYks", resquestBody, response))
        return false;

    QJsonArray maps = response["mapsigs"].toArray();
    for(auto it : maps) {
        QSharedPointer<IModel> info(new CModbusUnitCoil(ESigType::EYk));
        info->fromJson(it.toObject());
        sigs.append(info);
    }

    return true;
}

bool CWebRequestApi::changeModbusMapYks(QString mapid, QMap<QString, QSharedPointer<IModel> > &sigs) {
    QJsonArray array;
    for(auto& it : sigs) {
        array.append(it->toJson());
    }
    QJsonObject resquestBody;
    resquestBody["map_id"] = mapid;
    resquestBody["mapsigs"] = array;

    QJsonObject response;
    if(!sendBlockPostRequest(ADDR + "/api/protocol/changeModbusMapYks", resquestBody, response))
        return false;

    QJsonObject faild_info = response["faild_info"].toObject();

    bool opt_add = true;
    bool opt_change = true;
    bool opt_delete = true;
    if(faild_info.contains("opt_add")) {
        opt_add = faild_info["opt_add"].toBool();
    }
    if(faild_info.contains("opt_change")) {
        opt_change = faild_info["opt_change"].toBool();
    }
    if(faild_info.contains("opt_delete")) {
        opt_delete = faild_info["opt_delete"].toBool();
    }

    for(auto& it : sigs) {
        CModbusUnitCoil* info = static_cast<CModbusUnitCoil*>(it.data());
        if(info->opt_type == EOptType::EAdd) {
            if(opt_add) {
                info->commit();
            }
        } else if(info->opt_type == EOptType::EChange) {
            if(opt_change) {
                info->commit();
            }
        } else if(info->opt_type == EOptType::EDelete) {
            if(opt_delete) {
                info->deleteReset();
            }
        }
    }

    return true;
}

bool CWebRequestApi::getModbusMapParameters(const QString userid, const QString modelName, QString mapid, QList<QSharedPointer<IModel> > &sigs) {
    QJsonObject resquestBody;
    resquestBody[userFieldName(USER_ENUM::EUserId)] = userid;
    resquestBody[baseSigFieldName(BASE_SIG_ENUM::EModel_name)] = modelName;
    resquestBody["map_id"] = mapid;

    QJsonObject response;
    if(!sendBlockPostRequest(ADDR + "/api/protocol/getModbusMapParameters", resquestBody, response))
        return false;

    QJsonArray maps = response["mapsigs"].toArray();
    for(auto it : maps) {
        QSharedPointer<IModel> info(new CModbusUnitRegister(ESigType::EParameter));
        info->fromJson(it.toObject());
        sigs.append(info);
    }

    return true;
}

bool CWebRequestApi::changeModbusMapParameters(QString mapid, QMap<QString, QSharedPointer<IModel> > &sigs)
{
    QJsonArray array;
    for(auto& it : sigs) {
        array.append(it->toJson());
    }
    QJsonObject resquestBody;
    resquestBody["map_id"] = mapid;
    resquestBody["mapsigs"] = array;

    QJsonObject response;
    if(!sendBlockPostRequest(ADDR + "/api/protocol/changeModbusMapParameters", resquestBody, response))
        return false;

    QJsonObject faild_info = response["faild_info"].toObject();

    bool opt_add = true;
    bool opt_change = true;
    bool opt_delete = true;
    if(faild_info.contains("opt_add")) {
        opt_add = faild_info["opt_add"].toBool();
    }
    if(faild_info.contains("opt_change")) {
        opt_change = faild_info["opt_change"].toBool();
    }
    if(faild_info.contains("opt_delete")) {
        opt_delete = faild_info["opt_delete"].toBool();
    }

    for(auto& it : sigs) {
        CModbusUnitRegister* info = static_cast<CModbusUnitRegister*>(it.data());
        if(info->opt_type == EOptType::EAdd) {
            if(opt_add) {
                info->commit();
            }
        } else if(info->opt_type == EOptType::EChange) {
            if(opt_change) {
                info->commit();
            }
        } else if(info->opt_type == EOptType::EDelete) {
            if(opt_delete) {
                info->deleteReset();
            }
        }
    }

    return true;
}




